using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using EnvDTE;
using System.Diagnostics;
using System.Globalization;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.Common;
using System.IO;
using Microsoft.Practices.RecipeFramework.Services;
using System.Collections.Specialized;
using System.Reflection;

namespace VSSDKAssist.Assets.Actions.Base
{
	/// <summary>
	/// Action base class.
	/// </summary>
	public abstract class ActionBase : ConfigurableAction
	{
		#region Fields
		DTE visualStudio; 
		#endregion

		#region Properties
		/// <summary>
		/// Gets the visual studio.
		/// </summary>
		/// <value>The visual studio.</value>
		protected DTE VisualStudio
		{
			get
			{
				return visualStudio;
			}
		} 
		#endregion

		#region Public Implementation
		/// <summary>
		/// Executes this instance.
		/// </summary>
		public override sealed void Execute()
		{
			OnExecute();
		}

		/// <summary>
		/// Undoes this instance.
		/// </summary>
		public override sealed void Undo()
		{
			OnUndo();
		}

		/// <summary>
		/// Configures attributes specified for this action
		/// </summary>
		/// <param name="attributes"></param>
		public override void Configure(System.Collections.Specialized.StringDictionary attributes)
		{
			base.Configure(attributes);
			OnConfigure(attributes);
		} 
		#endregion

		#region Protected Implementation
		/// <summary>
		/// Called when [sited].
		/// </summary>
		protected override void OnSited()
		{
			base.OnSited();
			visualStudio = this.GetService<DTE>(true);
		}

		/// <summary>
		/// Called when [execute].
		/// </summary>
		protected virtual void OnExecute()
		{ }

		/// <summary>
		/// Called when [undo].
		/// </summary>
		protected virtual void OnUndo()
		{ }

		/// <summary>
		/// Called when [configure].
		/// </summary>
		/// <param name="attributes">The attributes.</param>
		protected virtual void OnConfigure(System.Collections.Specialized.StringDictionary attributes)
		{ }

		private string solutionBasePath;

		/// <summary>
		/// Gets the solution base path.
		/// </summary>
		/// <value>The solution base path.</value>
		protected virtual string SolutionBasePath
		{
			get
			{
				if(string.IsNullOrEmpty(solutionBasePath))
				{
					solutionBasePath = 
						Path.GetDirectoryName(VisualStudio.Solution.Properties.Item("Path").Value.ToString());
				}

				return solutionBasePath;
			}
		}

		private string packageBasePath;

		/// <summary>
		/// Gets the package base path.
		/// </summary>
		/// <value>The package base path.</value>
		protected virtual string PackageBasePath
		{
			get
			{
				if(string.IsNullOrEmpty(packageBasePath))
				{
					IConfigurationService configService = this.GetService<IConfigurationService>();
					packageBasePath = configService.BasePath;
				}

				return packageBasePath;
			}
		} 
		#endregion
	}
}